import { configuration } from '../../config/webapi';
import View from '../View';
import event from '../../util/NotifierManager';
import {
    INDEX_HEAT, INDEX_METER,
} from '../../util/constant';

/**
 * 智能仪表
 *
 * @export
 * @class Index3d
 * @extends {View}
 */
export default class Index2dMeter extends View {

    constructor(view, main) {
        super(view, main);

        this.url = 'displays/tenant/OPCUAWebAPIss/Dev/智能仪表.json'; // 智能仪表：displays/tenant/OPCUAWebAPIss/Dev/智能仪表.json
        this.key = INDEX_METER;

        this.tags = ['monthPlan', 'meter_chart1', 'meter_chart2', 'meter_chart3', 'naviNode'];
        this.btnTags = [];
        this.tags = this.tags.concat(this.btnTags);

        /** 对应着后端每一个数据 */
        this.tagMap = {
            yearPlan1: 10,
            yearPlan2: 10,
            yearPlan3: 10,

            monthPlan1: 10,
            monthPlan2: 10,
            monthPlan3: 10,
            monthPlan4: 10,

            dayPlan1: 10,
            dayPlan2: 10,
            dayPlan3: 10,
            dayPlan4: 10,

            storeData1: 10,
            storeData2: 10,
            storeData3: 10,

            meter_chart1_1: [],
            meter_chart1_2: [],

            meter_chart2_1: [],
            meter_chart2_2: [],

            meter_chart3_1: [],
            meter_chart3_2: [],

            meter_table: [], // len = 7x9 = 63
        }

        window.index2dMeter = this;
    }

    onPostDeserialize() {
        if (!ht.animationplayer.g2d) {
            ht.animationplayer.g2d = this.view;
            ht.animationplayer.player2d = new ht.animationplayer.blueprintplayer(view);
            ht.animationplayer.player2d.init();
        } else {
            ht.animationplayer.player2d.init(true);
        }

        this.getDatasByTag();

        this.naviNode.a('onStateChange', (state) => {
            this.onStateChange(state);
        });

        this.startRefreshTask();
        this.readAll();
    }

    handleInteractive(e) {
        const { kind, data } = e;
        if (kind == 'doubleClickBackground') {
            return;
        }
        if (kind == 'clickData') {
            this.handleClickData(data);
        }

    }

    handleClickData(data) {
        // console.log(data);
    }

    /******************************* OPCUA BEGIN ******************************** */

    handleUpdateBooleanNode(tag, valueObj) {
    }

    handleUpdateNumberNode(tag, valueObj) {
        // 年度计划
        if (tag.indexOf('yearPlan') >= 0) {
            this[tag].s('text', valueObj.Value);
        }

        // 月度计划
        if (tag.indexOf('monthPlan') >= 0) {
            this.monthPlan.a(tag, valueObj.Value);
        }

        // 日计划
        if (tag.indexOf('dayPlan') >= 0) {
            this[tag].s('text', valueObj.Value);
        }

        // 库存趋势
        if (tag == 'storeData1') {
            this[tag].s('text', valueObj.Value);
        }

        if (tag.indexOf('storeData') >= 0) {
            this[tag].a('value1', valueObj.Value);
        }
    }

    /**
     * 更新图表
     *
     * @param {*} tag tagMap中的tag
     * @param {*} valueObj .Value = [0.1,...], 数值范围是0~1之间的随机数
     * @memberof Index2dHeat
     */
    handleUpdateArrayNode(tag, valueObj) {
        // console.log('-----handleUpdateArrayNode-----', { tag, valueObj })

        switch (tag) {
            case 'meter_chart1_1':
                this.meter_chart1.a('lineData1', valueObj.Value.map((v) => { return v.toFixed(0) }));
                break;
            case 'meter_chart1_2':
                this.meter_chart1.a('lineData2', valueObj.Value.map((v) => { return v.toFixed(0) }));
                break;
            case 'meter_chart2_1':
                this.meter_chart2.a('lineData1', valueObj.Value.map((v) => { return v.toFixed(0) }));
                break;
            case 'meter_chart2_2':
                this.meter_chart2.a('lineData2', valueObj.Value.map((v) => { return v.toFixed(0) }));
                break;
            case 'meter_chart3_1':
                this.meter_chart3.a('lineData1', valueObj.Value.map((v) => { return v.toFixed(0) }));
                break;
            case 'meter_chart3_2':
                this.meter_chart3.a('lineData2', valueObj.Value.map((v) => { return v.toFixed(0) }));
                break;
            case 'meter_table':
                const obj = {};
                if (valueObj.Value) {
                    valueObj.Value.forEach((v, index) => {
                        obj['value' + index] = (v * 100).toFixed(0);
                    });
                }
                this.meter_table.a(obj);
                break;
            default:
                break;
        }
    }


}
